home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #14 / Monster Media No. 14 (April 1996) (Monster Media, Inc.).ISO / prog_d / oleauttr.zip / XLOLE5.ZIP / XLDRIV.PAS < prev    next >
Pascal/Delphi Source File  |  1996-01-04  |  3KB  |  102 lines

  1. unit Xldriv;
  2.  
  3. interface
  4.  
  5. uses
  6.   Oleauto, XLCls, SafeOle,
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, StdCtrls, ToCtrl, Menus;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     ScrollBox1: TScrollBox;
  13.     MainMenu1: TMainMenu;
  14.     OleAutoClient1: TOleAutoClient;
  15.     btnSaveSheet: TButton;
  16.     OleContainer1: TOleContainer;
  17.     btnQuit: TButton;
  18.     SaveDialog1: TSaveDialog;
  19.     edCellText: TEdit;
  20.     Label1: TLabel;
  21.     Label2: TLabel;
  22.     procedure btnQuitClick(Sender: TObject);
  23.     procedure btnSaveSheetClick(Sender: TObject);
  24.     procedure FormActivate(Sender: TObject);
  25. end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.   ExcelApp : TExcelApp;
  30.  
  31. implementation
  32.  
  33. {$R *.DFM}
  34.  
  35. procedure TForm1.btnSaveSheetClick(Sender: TObject);
  36. var
  37.   i : Integer;
  38.   XLNewBook : TXLWorkBook;
  39.   XLActiveSheet : TExcelSheet;
  40.   FirstCell : TExcelCells;
  41. begin
  42.   { Protection block for Ooblist memory allocation. }
  43.   try
  44.   { Create a list to keep track of created OLE objects. }
  45.   TSafeOleObject.CreateOobList;
  46.     try
  47.  
  48.       { Protection block for OLE Automation. }
  49.       try
  50.         { Tell OLE to start up Excel. }
  51.         ExcelApp := TExcelApp.GetObject('Excel.Application');
  52.         try
  53.           { ExcelApp.Visible := True; }
  54.  
  55.           XLNewBook := ExcelApp.WorkBooks.Add;
  56.           XLActiveSheet := XLNewBook.ActiveSheet;
  57.           FirstCell := XLActiveSheet.Cells[1,1];
  58.           FirstCell.Value := edCellText.Text;
  59.           if SaveDialog1.Execute then
  60.             XLActiveSheet.FileSave(SaveDialog1.FileName);
  61.           ExcelApp.Quit;
  62.         finally
  63.           { Call the destructor for the OLE object. }
  64.           ExcelApp.Release;
  65.           ExcelApp := nil; { So btnQuit doesn't try to use it. }
  66.         end;
  67.       except
  68.         ShowMessage('Caught an exception.  Excel may not be ' +
  69.             'installed correctly.  Otherwise, did you remember ' +
  70.             'to drop a TOLEAutoClient component on your form?');
  71.       end;
  72.  
  73.     finally
  74.       TSafeOleObject.FreeOobList;
  75.     end;
  76.   except
  77.     ShowMessage('Not enough memory for internal record keeping.');
  78.   end;
  79.  
  80.   Close;
  81. end;
  82.  
  83. procedure TForm1.btnQuitClick(Sender: TObject);
  84. begin
  85.   if Assigned(ExcelApp) then
  86.   begin
  87.     ExcelApp.Quit;
  88.     ExcelApp.Release;
  89.     ExcelApp := nil;
  90.   end;
  91.   Close;
  92. end;
  93.  
  94. procedure TForm1.FormActivate(Sender: TObject);
  95. begin
  96.   { Set focus to the TOLEContainer so the server is started. }
  97.   ActiveControl := OleContainer1;
  98. end;
  99.  
  100. end.
  101.  
  102.